package com.lgkj;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;

import com.clj.fastble.BleManager;
import com.meiqia.core.callback.OnInitCallback;
import com.meiqia.meiqiasdk.util.MQConfig;
import com.umeng.commonsdk.UMConfigure;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/6/15 0015.
 */

public class MyApplication extends Application {
    private static final String TAG = "MyApplication";
    //activity列表
    public static List<Activity> myActivitys = new ArrayList<>();
    private static Activity topActivity;


    private static Context context;

    public static Context getContext() {
        return context;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        registerActivityListener();
        initMQ();
        initFastBle();
        UMConfigure.init(this, UMConfigure.DEVICE_TYPE_PHONE, "");
        UMConfigure.setLogEnabled(false);
    }

    private void initMQ() {
        //367c6491e57120d38b165cf405f75fad
        MQConfig.init(this, "367c6491e57120d38b165cf405f75fad", new OnInitCallback() {
            @Override
            public void onSuccess(String clientId) {
//                Toast.makeText(getApplicationContext(), "init success", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(int code, String message) {
//                Toast.makeText(getApplicationContext(), "int failure", Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 蓝牙初始化
     */
    private void initFastBle() {
        BleManager.getInstance()
                .enableLog(true)
                .setReConnectCount(1, 5000)
                .setSplitWriteNum(20)
                .setConnectOverTime(10000)
                .setOperateTimeout(5000);
        BleManager.getInstance().init(this);
    }

    /**
     * 注册监听activity的创建和销毁
     */
    private void registerActivityListener() {
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle bundle) {
                //activity创建
                addActivity(activity);
            }

            @Override
            public void onActivityStarted(Activity activity) {
            }

            @Override
            public void onActivityResumed(Activity activity) {
                topActivity = activity;
            }

            @Override
            public void onActivityPaused(Activity activity) {
            }

            @Override
            public void onActivityStopped(Activity activity) {
            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
            }

            @Override
            public void onActivityDestroyed(Activity activity) {
                //activity销毁
                removeActivity(activity);
            }
        });
    }

    /**
     * 添加activity到列表
     *
     * @param activity 添加的activity
     */
    public void addActivity(Activity activity) {
        if (myActivitys == null)
            return;
        myActivitys.add(activity);
    }

    /**
     * 从列表移除指定activity
     *
     * @param activity 移除的activity
     */
    public void removeActivity(Activity activity) {
        if (myActivitys == null || myActivitys.isEmpty())
            return;
        myActivitys.remove(activity);
    }

    /**
     * 根据类名从列表获取activity
     *
     * @param className 类名
     * @return 指定activity，没有则返回 null
     */
    public static Activity getActivity(String className) {
        Activity myActivity = null;
        if (myActivitys != null) {
            for (Activity activity : myActivitys)
                if (activity.getClass().getName().equals(className))
                    myActivity = activity;
        }
        return myActivity;
    }

    /**
     * 销毁指定activity
     * 81800+7000+4000=92800+6000
     * <p>
     * 64000+25000
     *
     * @param activity 销毁的activity
     */
    public static void finishActivity(Activity activity) {
        if (myActivitys == null || myActivitys.isEmpty())
            return;
        if (activity != null) {
            myActivitys.remove(activity);
            activity.finish();
        }
    }

    /**
     * 销毁所有activity
     */
    public static void finishAllActivity() {
        if (myActivitys == null || myActivitys.isEmpty())
            return;
        for (Activity activity : myActivitys)
            activity.finish();
        myActivitys.clear();
    }

    /**
     * 应用在前台时，当前页面
     *
     * @return
     */
    public static Activity getTopActivity() {
        return topActivity;
    }

}
